using System.Diagnostics;
using System.Net.Http.Headers;
using YMJake.Flowable.ExternalWorker.Client.AspNetCore.Configuration;
using YMJake.Flowable.ExternalWorker.Client.Core.Rest;

namespace YMJake.Flowable.ExternalWorker.Client.Core.Client;

/// <summary>
/// Main client for interacting with Flowable External Worker API
/// </summary>
public class ExternalWorkerClient : IExternalWorkerClient
{
    private readonly FlowableExternalWorkerRestClient _restClient;

    // Primary constructor for dependency injection
    public ExternalWorkerClient(FlowableExternalWorkerOptions options)
    {
        options.SetupAuthentication();
        _restClient = new FlowableExternalWorkerRestClient(
            options.FlowableHost,
            options.WorkerId ?? GetDefaultWorkerId(),
            options.Authentication,
            options.CustomizeClient
        );
    }

    // Simplified constructor for basic usage
    public ExternalWorkerClient(
        string flowableHost,
        string? workerId = null,
        AuthenticationHeaderValue? authentication = null,
        Action<HttpClient>? customizeClient = null)
    {
        _restClient = new FlowableExternalWorkerRestClient(
            flowableHost,
            workerId ?? GetDefaultWorkerId(),
            authentication,
            customizeClient
        );
    }
    
    public FlowableExternalWorkerRestClient RestClient => _restClient;
    
    private static string GetDefaultWorkerId()
    {
        return $"{Environment.MachineName}-{Process.GetCurrentProcess().Id}";
    }
}